import { createStore, applyMiddleware, compose  } from 'redux'
import { routerMiddleware } from 'react-router-redux'

import thunk from 'redux-thunk'

import makeRootReducer from '../reducers'

// const createStoreWithMiddleware = compose(
//     applyMiddleware(
//         thunk
//     ),
//     window.devToolsExtension ? window.devToolsExtension() : f => f
// )(createStore)

export default function configureStore(initialState,history) {
    const middleware = [thunk, routerMiddleware(history)]
    const enhancers = []

    const devToolsExtension = window.devToolsExtension
    if (typeof devToolsExtension === 'function') {
        enhancers.push(devToolsExtension())
    }

    const store = createStore(
        makeRootReducer(),
        initialState,
        compose(
            applyMiddleware(...middleware),
            ...enhancers
        )
    )
    store.asyncReducers = {}

    if (module.hot) {
        module.hot.accept('../reducers', () => {
            const reducers = require('../reducers').default
            store.replaceReducer(reducers(store.asyncReducers))
        })
    }

    return store
}
